{% extends 'base_backend.html' %}
{% load static %}
{% load my_filter %}

{% block header_tail %}
    <link rel="stylesheet" href="{% static 'zhongbao/zhongbao_admin/css/style.css' %}">
    <link rel="stylesheet" href="{% static 'zhongbao/zhongbao_admin/css/iconfont.css' %}">
    <link rel="stylesheet" href="{% static 'plugins/datatables/dataTables.bootstrap.css' %}">
    <link rel="stylesheet" href="{% static 'public/ComboSelect/css/combo.select.css' %}">
    <link rel="stylesheet" href="{% static 'zhongbao/config/css/main.css' %}">
    <link rel="stylesheet" href="{% static 'laydate/theme/default/laydate.css' %}">
    <script src="{% static 'plugins\jQuery\jquery-2.2.3.min.js' %}"></script>

    <script src="{% static 'plugins/vue1/vue.js' %}"></script>
    <script src="{% static 'laydate/laydate.js' %}"></script>


    <style>
        .login-banner {
            width: 100%;
            background: url("{% static 'public/img/banner2.jpg' %}") center bottom/cover no-repeat;
        }
    </style>
{% endblock header_tail %}
{% block section_content %}
    <div style="margin-right: -15px; margin-left: -15px; margin-bottom: -20px;">
        <div class="col-lg-2 col-xs-4">
            <div class="small-box bg-green">
                <div class="inner">
                    <p>处理中</p>
                    <h3 id="status2">{{ counts.2 }}<sup style="font-size: 20px"></sup></h3>
                </div>
                <div class="icon" style="padding-top: 15px;">
                    <i class="fa fa-sitemap"></i>
                </div>
                <a href="{% url 'task:mine' %}?status=2" class="small-box-footer">
                    详情&nbsp;
                    <i class="fa fa-arrow-circle-right"></i>
                </a>
            </div>
        </div>
        <div class="col-lg-2 col-xs-4">
            <div class="small-box bg-yellow">
                <div class="inner">
                    <p>待审核</p>
                    <h3 id="status3">{{ counts.3 }}</h3>
                </div>
                <div class="icon" style="padding-top: 15px;">
                    <i class="fa fa-list"></i>
                </div>
                <a href="{% url 'task:mine' %}?status=3" class="small-box-footer">
                    详情&nbsp;
                    <i class="fa fa-arrow-circle-right"></i>
                </a>
            </div>
        </div>
        <div class="col-lg-2 col-xs-4">
            <div class="small-box bg-red">
                <div class="inner">
                    <p>已合格</p>
                    <h3 id="status4">{{ counts.4 }}</h3>
                </div>
                <div class="icon" style="padding-top: 15px;">
                    <i class="fa fa-bell"></i>
                </div>
                <a href="{% url 'task:mine' %}?status=4" class="small-box-footer">
                    详情&nbsp;
                    <i class="fa fa-arrow-circle-right"></i>
                </a>
            </div>
        </div>
        <div class="col-lg-2 col-xs-4">
            <div class="small-box bg-purple-gradient">
                <div class="inner">
                    <p>未合格</p>
                    <h3 id="status5">{{ counts.5 }}</h3>
                </div>
                <div class="icon" style="padding-top: 15px;">
                    <i class="fa fa-book"></i>
                </div>
                <a href="{% url 'task:mine' %}?status=5" class="small-box-footer">
                    详情&nbsp;
                    <i class="fa fa-arrow-circle-right"></i>
                </a>
            </div>
        </div>
        <div class="col-lg-2 col-xs-4">
            <div class="small-box bg-green">
                <div class="inner">
                    <p>已通过</p>
                    <h3 id="status11">{{ counts.6 }}<sup style="font-size: 20px"></sup></h3>
                </div>
                <div class="icon" style="padding-top: 15px;">
                    <i class="fa fa-sitemap"></i>
                </div>
                <a href="{% url 'task:mine' %}?status=11" class="small-box-footer">
                    详情&nbsp;
                    <i class="fa fa-arrow-circle-right"></i>
                </a>
            </div>
        </div>
        <div class="col-lg-2 col-xs-4">
            <div class="small-box bg-green">
                <div class="inner">
                    <p>已结算</p>
                    <h3 id="status9">{{ counts.7 }}<sup style="font-size: 20px"></sup></h3>
                </div>
                <div class="icon" style="padding-top: 15px;">
                    <i class="fa fa-sitemap"></i>
                </div>
                <a href="{% url 'task:mine' %}?status=6" class="small-box-footer">
                    详情&nbsp;
                    <i class="fa fa-arrow-circle-right"></i>
                </a>
            </div>
        </div>
        <!--         <div class="col-lg-1 col-xs-4">-->
        <!--            <div class="small-box bg-green">-->
        <!--                <div class="inner">-->
        <!--                    <p>已回收</p>-->
        <!--                    <h3 id="status0">{{ counts.0 }}<sup style="font-size: 20px"></sup></h3>-->
        <!--                </div>-->
        <!--                <div class="icon" style="padding-top: 15px;">-->
        <!--                    <i class="fa fa-sitemap"></i>-->
        <!--                </div>-->
        <!--                <a href="{% url 'task:mine' %}?status=0" class="small-box-footer">-->
        <!--                    详情&nbsp;-->
        <!--                    <i class="fa fa-arrow-circle-right"></i>-->
        <!--                </a>-->
        <!--            </div>-->
        <!--        </div>-->
    </div>
    <div>
        <div class="box-header with-border">
            <h3 class="box-title">{{ model_verbose_name }}</h3>
        </div>
        <!-- form start -->
        <div class="box box-primary">
            <div class="box-header with-border" style="border-color: #1b6d85">

                <!-- Date range -->
                <form role="form" id="searchForm" style="clear:both;">
                    <input class="no-display" type="text">
                    <p style="clear: both"></p>
                    <div style="float:left; width:95%;">
                        <div style="float: left;" class="input-group select-config-div">
                            <label for="id" class="control-label">任务ID：</label>
                            <p>
                                <input id="taskId" name="id" type="text" class="config-input">
                            </p>

                        </div>
                        {% for field in form %}
                            <div class="input-group select-config-div" style="float: left; ">
                                <label for="" class="control-label">{{ field.label }}：</label>
                                <p>{{ field }}</p>
                            </div>
                        {% endfor %}

                        <div style="float: left;" class="input-group select-config-div">
                            <label for="id" class="control-label">任务状态：</label>
                            <select name="status" id="taskStatus" class="config-input">
                                {% for filter in filters %}
                                    {% if filter.code == status %}
                                        <option selected="selected"
                                                value="{{ filter.code }}">{{ filter.description }}</option>
                                    {% else %}
                                        <option value="{{ filter.code }}">{{ filter.description }}</option>
                                    {% endif %}
                                {% endfor %}
                            </select>
                        </div>
                        <div style="float: left;" class="input-group select-config-div">
                            <label for="id" class="control-label">任务不合格次数：</label>
                            <p>
                                <input id="disqualify_num" name="disqualify_num" type="number" class="config-input">
                            </p>
                        </div>
                        <div style="float: left;" class="input-group select-config-div">
                            <label for="id" class="control-label">是否提交质检：</label>
                            <select name="is_submit_quality" id="is_submit_quality" class="config-input">
                                <option selected="selected" value="">全部</option>
                                <option value="True">是</option>
                                <option value="False">否</option>
                            </select>
                        </div>
                        <div style="float: left;" class="input-group select-config-div">
                            <label for="id" class="control-label">任务优先级：</label>
                            <select name="priority" id="priority" class="config-input">
                                <option selected="selected" value="">全部</option>
                                <option value="特急">特急</option>
                                <option value="高级">高级</option>
                                <option value="中级">中级</option>
                                <option value="低级">低级</option>
                            </select>
                        </div>
                        <div style="float: left;" class="input-group select-config-div">
                            <label for="id" class="control-label">不合格原因：</label>
                            <select name="disqualify_reason_new" id="disqualify_reason_new" class="config-input">
                                    <option selected="selected" value="">全部</option>
                                    <option value="备案号未填写">备案号未填写</option>
                                    <option value="代码错误">代码错误</option>
                                    <option value="配置分组错误">配置分组错误</option>
                                    <option value="配置编码">配置编码</option>
                                    <option value="采集时限错误">采集时限错误</option>
                                    <option value="info_flag错误">info_flag错误</option>
                                    <option value="国家错误">国家错误</option>
                                    <option value="语言错误">语言错误</option>
                                    <option value="域名错误">域名错误</option>
                                    <option value="url错误">url错误</option>
                                    <option value="title错误">title错误</option>
                                    <option value="ctime错误">ctime错误</option>
                                    <option value="source错误">source错误</option>
                                    <option value="source_id错误">source_id错误</option>
                                    <option value="retweeted_source错误">retweeted_source错误</option>
                                    <option value="retweeted_status_url错误">retweeted_status_url错误</option>
                                    <option value="channel错误">channel错误</option>
                                    <option value="pic_urls错误">pic_urls错误</option>
                                    <option value="sitename错误">sitename错误</option>
                                    <option value="visitCount错误">visitCount错误</option>
                                    <option value="replyCount错误">replyCount错误</option>
                                    <option value="content错误">content错误</option>
                                    <option value="content_xml错误">content_xml错误</option>
                                    <option value="audio_urls错误">audio_urls错误</option>
                                    <option value="video_urls错误">video_urls错误</option>
                                    <option value="face_img错误">face_img错误</option>
                                    <option value="is_junk错误">is_junk错误</option>
                                    <option value="网站名称内外不一致">网站名称内外不一致</option>
                                    <option value="其他">其他</option>
                                </select>
                        </div>
                        <div style="float: left;" class="input-group select-config-div">
                            <label for="id" class="control-label">每页显示：</label>
                            <select name="limit" id="showNumSelect" class="config-input">
                                {% for i in limits %}
                                    {% if i == limit %}
                                        <option selected="selected" value="{{ i }}">{{ i }}条</option>
                                    {% else %}
                                        <option value="{{ i }}">{{ i }}条</option>
                                    {% endif %}
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="input-group" style="width: 5%; float: right; margin-top: 70px;">
                        <input type="text" class="no-display">
                        <span id="searchBtn" onclick="searchTasks()"
                              class="form-control pull-left btn btn-sm btn-primary"
                              style="margin-top:20px;width: 50px">检索
                        </span>
                    </div>
                    <p style="clear: both"></p>
                    <div class="form-group">
                    </div>
                </form>
            </div>

            <!--
                <div class="box-header with-border" style="border-color: #1b6d85">
                </div>
                <div id="ItemsTree" style="position:absolute;left:200px;top:90px; display:none;width:250px;height:300px;border:dashed 1px #000000; background-color:#F7F7F7; overflow:auto"></div>
            -->
            <!-- 展示搜索出来的任务 -->
            <div id="main">
                <div id="showMessage" v-show="message"> [[ message ]]</div>
                <div class="box-body" v-show="showMain">
                    <form id="model_form" role="form"
                          method="post">{% csrf_token %}
                        <div class="group">
                            <table id="task_each_table" border="1"
                                   class="table table-bordered table-hover table-striped">
                                <thead>
                                <tr>
                                    <th v-for="head in tableData.headers">
                                        <span style="float: left">
                                            [[ head.name ]]
                                        </span>
                                        {% if field.rank %}
                                            <a href="?[[ head.rank ]]=[[ head.rank_tag ]][[ page_args ]]"
                                               style="float: right">
                                                {% if field.rank_tag == 0 %}
                                                    <i class="fa fa-fw fa-sort"></i>
                                                {% elif field.rank_tag == 1 %}
                                                    <i class="fa fa-fw fa-sort-numeric-asc"></i>
                                                {% elif field.rank_tag == -1 %}
                                                    <i class="fa fa-fw fa-sort-numeric-desc"></i>
                                                {% endif %}
                                            </a>
                                        {% endif %}
                                    </th>
                                    <th>
                                        <span style="float: left">
                                            操作
                                        </span>
                                    </th>
                                </tr>
                                </thead>
                                <tbody>
                                <template v-for="task in tableData.tasks">
                                    <!--<tr v-if="task.allocation_flag">-->
                                    <tr>
                                        <template v-for="head in tableData.headers">
                                            <template
                                                    v-if="head.key == 'allocation_flag' || head.key == 'receive_flag' || head.key == 'deadline_flag' || head.key == 'finish_flag' || head.key == 'ctime' || head.key == 'qualified_passed_flag' || head.key == 'status'">
                                                <td v-if="head.key == 'status'">[[ parseStatus(task[head.key]) ]]</td>
                                                <td v-else>[[ parseTime(task[head.key]) ]]</td>
                                            </template>
                                            <template v-else>
                                                <td v-if="head.key == 'site_url'"><a href="[[ task[head.key] ]]"
                                                                                     target="_Blank">[[
                                                    truncatechars(task[head.key], 30) ]]</a></td>
                                                <td v-else title="[[ task[head.key] ]]">[[ truncatechars(task[head.key],
                                                    10) ]]
                                                    <span v-if="head.key == 'disqualify_reason_new' && [[ task[head.key] ]] != ''"
                                                          id="copy_button"
                                                          style="background: #d2c3c3; font-size: 2px; cursor: pointer"
                                                          onclick="copy_text('[[ task[head.key] ]]')">复制</span>
                                                </td>
                                            </template>
                                        </template>
                                        <td class="btn-group">
                                            <button type="button" value="[[ task.id ]]" v-if="task.is_distort != 1 && task.is_distort != 2" class="btn btn-info btn-xs"
                                                    onclick="detailOneTask([[task.id]])">详 情
                                            </button>
                                            <button type="button" v-if="task.status == 2" value="[[ task.id ]]"
                                                    class="btn btn-info btn-xs" onclick="visualization([[task.id]])">
                                                可视化配置（公测）
                                            </button>
                                        </td>
                                    </tr>
                                </template>
                                </tbody>
                            </table>
                            <input id="copy_text_input" type="text" value=""
                                   style="position: absolute; top: 0; left: 0; opacity: 0; z-index: -10">
                            <p class="" id="paginator">
                                <a class="btn btn-default btn-xs" v-if="paginator.current > 1"
                                   onclick="toPage('first',paginator.limit)">首页</a>
                                <a class="btn btn-default btn-xs" v-else disabled="true">首页</a>
                                <a class="btn btn-default btn-xs" v-if="paginator.current > 1"
                                   onclick="toPage('prev',paginator.limit)">上一页</a>
                                Page [[ paginator.current ]] of [[ paginator.last ]]
                                <span v-show="total">共[[ total ]]个</span>
                                <a class="btn btn-default btn-xs" v-if="paginator.current < paginator.last"
                                   onclick="toPage('next',paginator.limit)">下一页</a>
                                <a class="btn btn-default btn-xs" v-if="paginator.current < paginator.last"
                                   onclick="toPage('last',paginator.limit)">尾页</a>
                                <a class="btn btn-default btn-xs" v-else disabled="true">尾页</a>
                                <input type="text" id="page-judge" style="width: 30px;height:20px; margin-left: 20px"
                                       onkeyup="toPage('judge', paginator.limit)">
                                <a class="btn btn-default btn-xs" onclick="toPage('judge', paginator.limit)">跳页</a>
                            </p>
                        </div>
                        <!-- /.box-footer -->
                        <div class="box-footer">
                            <button class="btn btn-primary no-display" id="submit_button">确认</button>
                            <button type="button" class="btn btn-primary" id="save_submit" name="_save"
                                    style="display: none">sure
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        function acceptOneTask(tids) {
            accept(tids)
        }

        function accept(tids) {
            if (!tids) {
                var _tids = [];
                $("input[name='td_checkbox']:checked").each(
                    function () {
                        _tids.push($(this).val())
                    }
                );
                tids = _tids.join();
            }
            if (tids.length === 0) {
                return false
            } else {
                $.ajaxSetup({
                    data: {
                        csrfmiddlewaretoken: '{{ csrf_token }}',
                        tids: tids
                    }
                });
                $.post(
                    "{% url 'task:accept' %}",
                    function (response) {
                        alert(response.message);
                        mainVue.message = response.message;
                        toPage('current')
                    }
                )
            }
        }

        function detailOneTask(tid) {
            var url = "{% url 'task:detail' %}" + "?id=" + tid + "&type=task" + "&user=user";
            //window.location.href = url;
            window.open(url);
        }

        function checkAll(obj) {
            if ($(obj).is(":checked")) {
                $("input[name='td_checkbox']").prop("checked", true)
            } else {
                $("input[name='td_checkbox']").prop("checked", false)
            }

        }

        function checkOne(obj) {
            if ($("input[name='th_checkbox']").is(":checked")) {
                $("input[name='th_checkbox']").prop("checked", false)
            } else {
                var unChecked = 0;
                $("input[name='td_checkbox']").each(
                    function () {
                        if ($(this).is(":checked")) {

                        } else {
                            unChecked += 1
                        }
                    }
                );
                if (unChecked === 0) {
                    $("input[name='th_checkbox']").prop("checked", true)
                }
            }
        }

        Vue.config.delimiters = ["[[", "]]"];

        function toPage(page) {
            if (mainVue.paginator) {
                if (page == 'current') {
                    page = mainVue.paginator.current
                }
                if (page == 'next') {
                    page = mainVue.paginator.current + 1
                }
                if (page == 'prev') {
                    page = mainVue.paginator.current - 1
                }
                if (page == 'first') {
                    page = 1
                }
                if (page == 'last') {
                    page = mainVue.paginator.last
                }
                if (page == 'judge') {
                    page = $("#page-judge").val();
                }
                if (page < 1) {
                    page = 1;
                }
                if (page > mainVue.paginator.last) {
                    page = mainVue.paginator.last;
                }
                if (page < mainVue.paginator.first) {
                    page = mainVue.paginator.first;
                }
            } else {
                page = 1;
            }
            mainVue.page = page;
            render(mainVue);
        }

        function render(obj) {
            var searchForm = obj.searchForm + '&page=' + obj.page;
            $.get(
                "{% url 'task:filter_tasks' %}",
                searchForm,
                function (response) {
                    if (response) {
                        obj.showMain = true;
                        obj.tableData = response.tableData;
                        obj.paginator = response.paginator;
                        obj.message = response.message;
                        obj.total = response.total;
                        console.log(obj.paginator);
                    } else {
                        obj.showMain = false;
                    }
                }
            )
        }

        var mainVue = new Vue({
            el: "#main",
            data: {
                page: 1,
                status: 1,
                limit: {{ limit }},
                searchForm: "",
                checkedAll: false,
                showMain: false,
                tableData: {},
                paginator: {},
                total: "",
                message: ""
            },
            methods: {
                truncatechars: function (str, end) {
                    if (!str) {
                        return ""
                    }
                    if (end <= str.length) {
                        return str.slice(0, end) + '...'
                    } else {
                        return str
                    }
                },
                parseTime: function (timeStamp) {
                    if (!timeStamp) {
                        return ""
                    }
                    var newDate = new Date();
                    newDate.setTime(timeStamp * 1000);
                    return newDate.toLocaleDateString()
                },
                projectName: function (project_id) {
                    if (!timeStamp) {
                        return ""
                    }
                    var newDate = new Date();
                    newDate.setTime(timeStamp * 1000);
                    return newDate.toLocaleDateString()
                },
                parseStatus: function (status) {
                    var statuses = {
                        1: {
                            name: "待分配"
                        },
                        2: {
                            name: "处理中"
                        },
                        3: {
                            name: "待审核"
                        },
                        4: {
                            name: "已合格"
                        },
                        5: {
                            name: "不合格"
                        },
                        6: {
                            name: "已结算"
                        },
                        0: {
                            name: "已回收"
                        },
                        8: {
                            name: "回收申请"
                        },
                        9: {
                            name: "回收通过"
                        },
                        10: {
                            name: "回收驳回"
                        },
                        11: {
                            name: "已通过"
                        }
                    };
                    return statuses[status]["name"]
                },
                //searchTasks: searchTasks,
                getTasks: function () {
                    var searchForm = 'page=' + this.page + '&limit=' + this.limit + '&status=' + this.status;
                    $.get(
                        "{% url 'task:filter_tasks' %}",
                        searchForm,
                        function (response) {
                            if (response) {
                                this.showMain = true;
                                this.tableData = response.tableData;
                                this.paginator = response.paginator;
                                this.message = response.message;
                                this.total = response.total;
                                console.log(this.paginator);
                            } else {
                                this.showMain = false;
                            }
                        }
                    )
                }
            },
            watch: {
                {#                page: function(){#}
                {#                    render(this);#}
                {#                }#}
            }
        });

        function searchTasks() {
            mainVue.searchForm = $("#searchForm").serialize();
            $.get(
                "{% url 'task:filter_tasks' %}",
                mainVue.searchForm,
                function (response) {
                    if (response) {
                        mainVue.limit = response.limit;
                        mainVue.showMain = true;
                        mainVue.limits = response.limits;
                        mainVue.tableData = response.tableData;
                        mainVue.paginator = response.paginator;
                        mainVue.message = response.message;
                        mainVue.total = response.total;
                    } else {
                        mainVue.showMain = false;
                    }
                }
            )
        }

        searchTasks();

        function changeColor(id, color) {
            //$(id).parent().parent().parent().children().children().children().find("li[color='green']");
            $(id).parent().parent().addClass("active");
            $(id).children().css("color", color);
        }

        //$("#taskManager").addClass("active");
        $('#myTasks').addClass("active");
        changeColor("#mine", "#00FF7F");
        laydate.render({
            elem: '#test1'
            , range: true
        });
        laydate.render({
            elem: '#test2'
            , range: true
        });
        laydate.render({
            elem: '#test3'
            , range: true
        });
        laydate.render({
            elem: '#test4'
            , range: true
        });

        function visualization(tid) {
            var url = "{% url 'task:visualization' %}" + "?step=loops&tid=" + tid;
            window.open(url);
        }

        // 复制
        function copy_text(text) {
            $("#copy_text_input").val(text);
            $("#copy_text_input").select();
            document.execCommand('Copy');
            alert('复制成功')
        }
    </script>
{% endblock section_content %}
